package com.aiyige.page.my.sell;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.BaseActivity;
import com.aiyige.base.api.JsonUtil;
import com.aiyige.model.WalletDetailEntity;
import com.aiyige.model.request.GetWaitReceviceMoneyListRequest;
import com.aiyige.model.request.GetWalletDetailsListRequest;
import com.aiyige.model.response.WalletListResponse;
import com.aiyige.page.my.sell.adapter.WaitReceiveMoneyListAdapter;
import com.aiyige.page.my.sell.model.WaitReceiveMoneyModel;
import com.aiyige.page.my.sell.model.WaitReceiveMoneyResponse;
import com.aiyige.page.my.wallet.adapter.WalletDetailsListAdapter;
import com.aiyige.utils.ListUtil;
import com.aiyige.utils.StringUtils;
import com.aiyige.utils.widget.CommonDataView;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Call;

/**
 * 待收款
 */
@Route(path = ARouterConfig.WaitReceiveMoneyListPage, extras = ARouterConfig.EXTRA_NEED_LOGIN)
public class WaitReceiveMoneyListPage extends BaseActivity implements CommonDataView.RetrofitStub {


    @BindView(R.id.cdv)
    CommonDataView cdv;

    @BindView(R.id.tv_accumulated_receive_money)
    TextView tvAccumulatedReceiveMoney;

    @BindView(R.id.ll_accumulated_receive_money)
    LinearLayout ll_accumulated_receive_money;



    WaitReceiveMoneyListAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_wait_receive_money_list);
        ButterKnife.bind(this);
        initTitleLayout(getString(R.string.wait_receive_money));
        adapter = new WaitReceiveMoneyListAdapter();
        cdv.config(adapter, this);
        cdv.addItemDecoration(1, R.color.zwcolor_gray_f4f8f9);
        cdv.setRecyclerViewBgColor(ContextCompat.getColor(this, R.color.zwcolor_gray_f4f8f9));
        cdv.requestData();
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                String momentId = ((WaitReceiveMoneyModel) adapter.getData().get(position)).getMomentId();
                if (!isEmpty(momentId)) {
                    ARouter.getInstance().build(ARouterConfig.WaitReceiveMoneyDetailListPage).withString("momentId", momentId).navigation();
                }
            }
        });
    }


    @Override
    public Call<ResponseBody> call(long currentPage) {
        return GetWaitReceviceMoneyListRequest.newBuilder()
                .pageNum(currentPage + "")
                .build();
    }

    @Override
    public CommonDataView.HandleResult handleResponse(String respJson, List resultList) {
        try {
            WaitReceiveMoneyResponse response = JsonUtil.toObject(respJson, WaitReceiveMoneyResponse.class);
            if (response != null && response.getStatistics() != null) {
                tvAccumulatedReceiveMoney.setText(StringUtils.priceFormat(response.getStatistics().getAddupWaitPay()));
            }
            if (response != null && !ListUtil.isEmpty(response.getContent())) {
                resultList.addAll(response.getContent());
                return new CommonDataView.HandleResult(response.isLast());
            }
//            ll_accumulated_receive_money.setVisibility(View.GONE);
            return new CommonDataView.HandleResult(true);
        } catch (Exception e) {
            return new CommonDataView.HandleResult(e.getMessage());
        }
    }


}
